package com.itheima.organization.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.aom.pojo.Aom;
import com.itheima.aom.pojo.AomOrder;
import com.itheima.aom.pojo.AomUser;
import com.itheima.entity.PageResult;
import com.itheima.organization.dao.AomMapper;
import com.itheima.organization.service.AomOrderService;
import com.itheima.organization.service.AomService;
import com.itheima.organization.service.AomUserService;
import com.itheima.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class AomServiceImpl implements AomService {

    @Autowired
    private AomMapper aomMapper;
    @Autowired
    private AomService aomService;
    @Autowired
    private AomOrderService aomOrderService;
    @Autowired
    private AomUserService aomUserService;


    /**
     * 根据id查询农业组织
     * @param id
     * @return
     */
    @Override
    public Aom findById(Long id) {
       return aomMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据手机号查询农业组织
     * @param phone
     * @return
     */
    @Override
    public List<Aom> findByPhoneNu(String phone) {
        Example example = new Example(Aom.class);
        example.createCriteria().andEqualTo("phone", phone);
        List<Aom> aoms = aomMapper.selectByExample(example);
        return aoms;
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Aom> findPage(Map<String, Object> map, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = createExample(map);
        List<Aom> aomList = aomMapper.selectByExample(example);

        return (Page<Aom>) aomList;
    }

    /**
     * 增加农业组织
     *
     * @param aom
     */
    @Override
    public void addAom(Aom aom, AomOrder aomOrder) {

        //插入创建时间
        aom.setCreattime(DateUtils.dateToString(new Date()));
        //插入激活时间
        aom.setActivetime(DateUtils.dateToString(new Date()));

        String phone = aom.getPhone();
        //查询手机号是否占用
        List<Aom> aomList = aomService.findByPhoneNu(phone);

        if (StringUtils.isEmpty(phone)){
            throw new RuntimeException("没有录入手机号,无法根据手机号生成用户账号");
        }else if(!aomService.findByPhoneNu(phone).isEmpty()){
            throw new RuntimeException("此手机号已被占用,更换手机号");
        }

        //aomService.addAom(aom);
        aomMapper.insertSelective(aom);

        //构造aomOrder
        aomOrder.setAomid(aom.getId());
        //设置开始时间
        aomOrder.setStarttime(DateUtils.dateToString(new Date()));

        //生成一个默认用户
        AomUser aomUser = new AomUser();
        aomUser.setUsername(phone);
        aomUser.setPassword("123456");
        aomUser.setAomid(aom.getId());
        aomUser.setStatus("1");
        aomUserService.add(aomUser);

        //新增的时候生成订单  中间表插入数据(aomOrderService中写了)
        aomOrderService.add(aomOrder);
    }

    /**
     * 修改农业组织
     *
     * @param aom
     */
    @Override
    public void updateAom(Aom aom) {
        aomMapper.updateByPrimaryKeySelective(aom);
    }

    /**
     * 逻辑删除农业组织
     * @param aom
     */
    @Override
    public void deleteAom(Aom aom) {
        aomMapper.updateByPrimaryKeySelective(aom);
    }

    /**
     * 根据id查询中间表获取最新订单id
     * @param aomid
     * @return
     */
    @Override
    public List<Map> selectByAomId(Long aomid) {
        List<Map> list = aomMapper.selectByAomId(aomid);
        if (list.isEmpty()){
            return null;
        }
        return list;
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example = new Example(Aom.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null){
            //农业组织id
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))){
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            //农场名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            //机构代码
            if (searchMap.get("code") != null && !"".equals(searchMap.get("code"))){
                criteria.andEqualTo("code", searchMap.get("code"));
            }
            //地址
            if (searchMap.get("address") != null && !"".equals(searchMap.get("address"))){
                criteria.andLike("address", "%" + searchMap.get("address") + "%");
            }
            //contact
            if (searchMap.get("contact") != null && !"".equals(searchMap.get("contact"))){
                criteria.andEqualTo("contact", searchMap.get("contact"));
            }
            //phone
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))){
                criteria.andEqualTo("phone", searchMap.get("phone"));
            }
            //email
            if (searchMap.get("email") != null && !"".equals(searchMap.get("email"))){
                criteria.andEqualTo("email", searchMap.get("email"));
            }
            //creattime
            if (searchMap.get("creattime") != null && !"".equals(searchMap.get("creattime"))){
                criteria.andEqualTo("creattime", searchMap.get("creattime"));
            }
            //activetime
            if (searchMap.get("activetime") != null && !"".equals(searchMap.get("activetime"))){
                criteria.andEqualTo("activetime", searchMap.get("activetime"));
            }
            //status
            if (searchMap.get("status") != null && !"".equals(searchMap.get("status"))){
                criteria.andEqualTo("status", searchMap.get("status"));
            }
        }
        return example;
    }


}
